package com.aide.simplification.global;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;

//@Component("GlobalConfig")
//@RemoteProxy(creator = SpringCreator.class, creatorParams = @Param(name = "beanName", value = "GlobalConfig"), name = "GlobalConfig")
@RemoteProxy(name="GlobalConfig")
public class GlobalConfig {
	PropertiesConfiguration config;
	private long reloadPeriod;

	public GlobalConfig() throws ConfigurationException {
		config = new PropertiesConfiguration("global.properties");
		FileChangedReloadingStrategy fs = new FileChangedReloadingStrategy();
		fs.setConfiguration(config);
		if (this.reloadPeriod > 0) {
			fs.setRefreshDelay(this.reloadPeriod);
		}
		config.setReloadingStrategy(fs);
	}

	public GlobalConfig(String filepath) throws ConfigurationException {
		config = new PropertiesConfiguration(filepath);
		FileChangedReloadingStrategy fs = new FileChangedReloadingStrategy();
		fs.setConfiguration(config);
		if (this.reloadPeriod > 0) {
			fs.setRefreshDelay(this.reloadPeriod);
		}
		config.setReloadingStrategy(fs);
	}

	@RemoteMethod
	public String getString(String properties) {
		return config.getString(properties);
	}

	public int getInt(String properties) {
		return config.getInt(properties);
	}

	@SuppressWarnings("unchecked")
	public List getList(String properties) {
		return config.getList(properties);
	}

	@SuppressWarnings("unchecked")
	public Iterator<String> getKeys(String prefix) {
		return config.getKeys(prefix);
	}

	@SuppressWarnings("unchecked")
	public Iterator<String> subset(String prefix) {
		Configuration con = config.subset(prefix);
		return con.getKeys();
	}

	@SuppressWarnings("unchecked")
	public Iterator<String> getRegex(String regex,String prefix) {
		Iterator<String> it =null;
		if(!prefix.equals("")){
			it=config.getKeys(prefix);
		}else{
			it = config.getKeys();
		}
		Pattern pattern = Pattern.compile(regex);
		Map<String,String> map = new HashMap<String,String>();
		while (it.hasNext()) {
			String str = it.next();
			Matcher matcher = pattern.matcher(str);
			if (matcher.find()) {
				map.put(matcher.group(), "sorc");
			}
		}
		return map.keySet().iterator();
	}


	public long getReloadPeriod() {
		return reloadPeriod;
	}

	public void setReloadPeriod(long reloadPeriod) {
		this.reloadPeriod = reloadPeriod;
	}

	public Object getObject(String properties) {
		return config.getProperty(properties);
	}

	public boolean getBoolean(String properties) {
		return config.getBoolean(properties);
	}

	public void addProperty(String properties, Object object) {
		config.addProperty(properties, object);
	}

	public void setProperty(String properties, Object object) {
		config.setProperty(properties, object);
	}

	public boolean save() {
		try {
			config.save();
			return true;
		} catch (ConfigurationException e) {
			return false;
		}
	}

	@SuppressWarnings("unchecked")
	@RemoteMethod
	public List<Map<String, String>> getFormListMap(String filepath,
			String prefix) {
		if (filepath.equals("")) {
			filepath = config.getPath();
		}
		if (config.getString(filepath) != null
				&& !"".equals(config.getString(filepath))) {
			filepath = config.getString(filepath);
		}
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		try {
			PropertiesConfiguration configtemp = new PropertiesConfiguration(
					filepath);
			Iterator<String> iterator = null;
			if (prefix.equals("")) {
				iterator = configtemp.getKeys();
			} else {
				iterator = configtemp.getKeys(prefix);
				prefix += ".";
			}
			while (iterator != null && iterator.hasNext()) {
				String key = iterator.next();
				Map<String, String> map = new HashMap<String, String>();
				map.put("id", key.substring(prefix.length(), key.length()));
				map.put("value", configtemp.getString(key));
				list.add(map);
			}
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@RemoteMethod
	public boolean setFormRequest(String filepath, String prefix,
			HttpServletRequest request, HttpServletResponse response) {
		if (filepath.equals("")) {
			filepath = config.getPath();
		}
		if (config.getString(filepath) != null
				&& !"".equals(config.getString(filepath))) {
			filepath = config.getString(filepath);
		}
		if (!prefix.equals("")) {
			prefix += ".";
		}
		try {
			PropertiesConfiguration configtemp = new PropertiesConfiguration(
					filepath);
			Map<String, String[]> map = request.getParameterMap();
			for (Map.Entry<String, String[]> entry : map.entrySet()) {
				configtemp.setProperty(prefix + entry.getKey(), entry
						.getValue()[0]);
				configtemp.save();
			}
			return true;
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}
		return false;
	}

	@RemoteMethod
	public boolean setFormListMap(String filepath, String prefix,
			List<Map<String, String>> list) {
		if (filepath.equals("")) {
			filepath = config.getPath();
		}
		if (config.getString(filepath) != null
				&& !"".equals(config.getString(filepath))) {
			filepath = config.getString(filepath);
		}
		if (!prefix.equals("")) {
			prefix += ".";
		}
		try {
			PropertiesConfiguration configtemp = new PropertiesConfiguration(
					filepath);
			for (Map<String, String> map : list) {
				configtemp
						.setProperty(prefix + map.get("id"), map.get("value"));
				configtemp.save();
			}
			return true;
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}
		return false;
	}
}
